export default {
  namespaced: true,
  state: {
    token: '',
    key: '',
    userId: '',
    userInfo: '{}',
    location: '{}'
  },
  getters: {
    token: state => {
      if (!state.token) {
        try {
          const token = localStorage.getItem(process.env.VUE_APP_USER_TOKEN)
          state.token = token
        } catch (e) {
          console.error(e)
        }
      }
      return state.token
    },
    key: state => {
      if (!state.key) {
        try {
          const key = localStorage.getItem(process.env.VUE_APP_USER_KEY)
          state.key = key
        } catch (e) {
          console.error(e)
        }
      }
      return state.key
    },
    userId: state => {
      if (!state.userId) {
        try {
          const userId = localStorage.getItem(process.env.VUE_APP_USER_ID)
          state.userId = userId
        } catch (e) {
          console.error(e)
        }
      }
      return state.userId
    },
    userInfo: state => {
      if (!state.userInfo) {
        try {
          const userInfo = localStorage.getItem(process.env.VUE_APP_USER_INFO)
          state.userInfo = userInfo
        } catch (e) {
          console.error(e)
        }
      }
      return state.userInfo
    },
    location: state => {
      if (!state.location) {
        try {
          const location = localStorage.getItem(
            process.env.VUE_APP_USER_LOCATION
          )
          state.location = location
        } catch (e) {
          console.error(e)
        }
      }
      return state.location
    }
  },
  mutations: {
    setToken (state, token) {
      state.token = token
      localStorage.setItem(process.env.VUE_APP_USER_TOKEN, token)
    },
    setKey (state, key) {
      state.key = key
      localStorage.setItem(process.env.VUE_APP_USER_KEY, key)
    },
    setUserId (state, userId) {
      state.userId = userId
      localStorage.setItem(process.env.VUE_APP_USER_ID, userId)
    },
    setUserInfo (state, userInfo) {
      state.userInfo = userInfo
      localStorage.setItem(process.env.VUE_APP_USER_INFO, userInfo)
    },
    setLocation (state, location) {
      state.location = location
      localStorage.setItem(process.env.VUE_APP_USER_LOCATION, location)
    }
  }
}
